//
//  MyViewController.m
//  Manage
//
//  Created by 杨康 on 2017/10/30.
//  Copyright © 2017年 杨康. All rights reserved.
//

#import "MyViewController.h"
#import "ModifypasswordViewController.h"
#import "UserModel.h"
#import "InputViewController.h"
@interface MyViewController ()
{
    UIButton *_photoButton;
    UIButton *_nameButton;
    UILabel *_phoneLabel;
    NSArray *secArr;
    NSString *uid;
    
    
}
@property(nonatomic,strong)ykTableView *tableView;
@end

@implementation MyViewController
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
    [self getInfo];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    
    self.view.backgroundColor=[UIColor colorF];
    
    
    
    UIScrollView *myScrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-BOTTOM_HEIGHT)];
    myScrollView.contentSize=CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    myScrollView.showsVerticalScrollIndicator = NO;
    myScrollView.showsHorizontalScrollIndicator = NO;
    [self.view addSubview:myScrollView];
    
    if (@available(iOS 11.0,*)) {
        
        myScrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else
    {
        
    }
    
    
    
    
    
    
    UIView *bgView=[[UIView alloc]init];
    [myScrollView addSubview:bgView];
    bgView.backgroundColor=[UIColor colorQ];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.and.left.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 200));
    }];
    
    UIButton *photoBtn=[SingLeton endButton:nil andTitle:nil RequestSuccess:^(UIButton *btn) {
        
        
        
        [SingLeton PhotoAction:self RequestSuccess:^(UIImagePickerController *picker, NSDictionary<NSString *,id> *info) {
            //选择
            if (info) {
                
                UIImage* image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
                //先把图片转成NSData
                NSData *data=UIImageJPEGRepresentation(image, 0.5);
                // 设置图片名
                NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
                formatter.dateFormat = @"yyyy-MM-dd HH:mm:ss";
                NSString *fileName=[NSString stringWithFormat:@"%@.jpg",[formatter stringFromDate:[NSDate date]]];
                // 设置图片存储路径
                NSString *filePath=[NSTemporaryDirectory() stringByAppendingPathComponent:fileName];
                [data writeToFile:filePath atomically:YES];
                
                NSString *url=[NSString stringWithFormat:@"%@/axq/admin/saveimg",APPBaseUrl];
                
                NSDictionary *imageDic = @{fileName:filePath};
                
                [NetworkPath MultiPartRequest:url andParameters:nil andValueFilePathWithKeyFileName:imageDic RequestSuccess:^(NSURLResponse *response, id responseObject) {
                    NSString *code = responseObject[@"code"];
                    if([code isEqualToString:@"10008"]){
                        
                       [btn setBackgroundImage:image forState:UIControlStateNormal];
                    }
                    else
                    {
                        NSString *str=responseObject[@"msg"];
                        
                        [self.view makeToast:str];
                    }
                } RequestFailed:^(NSError *error) {
                    
                }];
            }
            //取消
            else
            {
                
            }
        }];
        
        
        
        
        
        
        
        
    } ];
    [bgView addSubview:photoBtn];
    _photoButton=photoBtn;
    [photoBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(48);
        make.centerX.mas_equalTo(bgView);
        make.size.mas_equalTo(CGSizeMake(60, 60));
    }];
    photoBtn.layer.cornerRadius=30.0;
    photoBtn.layer.masksToBounds=YES;
    photoBtn.layer.borderWidth=1;
    photoBtn.layer.borderColor=[UIColor colorWithRed:79.0f/255.0f green:177.0f/255.0f blue:252.0f/255.0f alpha:1.0].CGColor;
    [photoBtn setBackgroundImage:[UIImage imageNamed:@"默认头像"] forState:UIControlStateNormal];
    
    
    
    UIImageView *tipImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"相机"]];
    [bgView addSubview:tipImg];
    [tipImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(photoBtn);
        make.bottom.mas_equalTo(photoBtn);
    }];
    
    
    
    
    
    
    
    
    
    
    YKButton *nameBtn=[SingLeton endButton:nil andTitle:@"账户名称" RequestSuccess:^(UIButton *btn) {
        InputViewController *vc=[[InputViewController alloc]init];
        vc.type=@"账户名称";
        vc.content=btn.titleLabel.text;
        if (uid) {
            vc.uid=uid;
        }
        [self.navigationController pushViewController:vc animated:YES];
    }];
    [bgView addSubview:nameBtn];
    [nameBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView);
        make.top.mas_equalTo(photoBtn.mas_bottom);
        
    }];
    nameBtn.titleLabel.font=[UIFont systemFontOfSize:16.0];
    _nameButton=nameBtn;
    
    
    
    
    
    UILabel *phoneLab=[SingLeton myLabel:[UIColor whiteColor] andTitle:@"13800000000" andFont:14.0];
    _phoneLabel=phoneLab;
    [bgView addSubview:phoneLab];
    [phoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView);
        make.top.mas_equalTo(nameBtn.mas_bottom);
    }];
    
    
    
    NSString *str;
    if ([APPTOKEN isEqualToString:@"admin"]) {
        secArr=[NSArray arrayWithObjects:@"联系人",@"联系电话", nil];
        str=@"管理员";
    }
    else if ([APPTOKEN isEqualToString:@"shop"])
    {
      secArr=[NSArray arrayWithObjects:@"门店",@"地址",@"联系人",@"联系电话", nil];
        str=@"门店";
    }
    else if ([APPTOKEN isEqualToString:@"producer"])
    {
        secArr=[NSArray arrayWithObjects:@"电动车厂商",@"地址",@"联系电话", nil];
        str=@"电动车厂商";
    }
    else if ([APPTOKEN isEqualToString:@"police"])
    {
        secArr=[NSArray arrayWithObjects:@"民警姓名",@"联系电话", nil];
        str=@"民警";
    }
    else if ([APPTOKEN isEqualToString:@"property"])
    {
        secArr=[NSArray arrayWithObjects:@"物业名称",@"联系人",@"联系电话", nil];
        str=@"物业";
    }
    else if ([APPTOKEN isEqualToString:@"repair"])
    {
        
    }
    UILabel *roleLab=[SingLeton myLabel:[UIColor whiteColor] andTitle:str andFont:14.0];
    
    [bgView addSubview:roleLab];
    [roleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(bgView);
        make.top.mas_equalTo(phoneLab.mas_bottom).mas_offset(10);
    }];
    
    
    //修改密码
    UIButton *modifyPwBtn=[SingLeton endButton:[UIColor whiteColor] andTitle:@"修改密码" RequestSuccess:^(UIButton *btn) {
        ModifypasswordViewController *vc=[[ModifypasswordViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }];
    [myScrollView addSubview:modifyPwBtn];
    [modifyPwBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(bgView.mas_bottom).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 44));
    }];
    [modifyPwBtn setImage:[UIImage imageNamed:@"修改密码"] forState:UIControlStateNormal];
    [self someSet:modifyPwBtn];
    
    
    
    self.tableView=[SingLeton myTableViewWithFrame:CGRectZero withRefresh:NO didSelect:^(ykTableView *tableView, NSIndexPath *indexPath) {
        UITableViewCell *cell=[tableView cellForRowAtIndexPath:indexPath];

            InputViewController *vc=[[InputViewController alloc]init];
            vc.type=cell.textLabel.text;
        vc.content=cell.detailTextLabel.text;
        if (uid) {
            vc.uid=uid;
        }
            [self.navigationController pushViewController:vc animated:YES];

    }];
    [myScrollView addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(modifyPwBtn.mas_bottom).mas_offset(10);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 44*secArr.count));
    }];
    
    self.tableView.className=@"Type2";
    NSMutableArray *itemArr=[NSMutableArray array];
    
    for (int i=0; i<secArr.count; i++) {
        cellData *dataObj=[[cellData alloc]init];
        dataObj.image=secArr[i];
    
        dataObj.text=secArr[i];
        if ([secArr[i] isEqualToString:@"门店"]) {
            dataObj.text=@"门店名称";
        }else if ([secArr[i] isEqualToString:@"民警姓名"])
        {
            dataObj.image=@"姓名";
        }
        else if ([secArr[i] isEqualToString:@"物业名称"])
        {
            dataObj.image=@"物业";
        }
        dataObj.detailText=secArr[i];
        [itemArr addObject:dataObj];
    }
    [self.tableView.dataArray addObject:itemArr];
    [self.tableView reloadData];
    
    
    
    //退出登录
    UIButton *logoutBtn=[SingLeton endButton:[UIColor whiteColor] andTitle:@"退出登录" RequestSuccess:^(UIButton *btn) {
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:@"退出登录" message:@"确定要退出登录吗?" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *noAction=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        
        UIAlertAction *someAction=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
           [NetworkPath leaveVC];
        }];
        
        
        [alertController addAction:noAction];
        [alertController addAction:someAction];
        [self presentViewController:alertController animated:YES completion:nil];
        [noAction setValue:[UIColor color3] forKey:@"titleTextColor"];

    }];
    [myScrollView addSubview:logoutBtn];
    [logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(self.tableView.mas_bottom).mas_offset(15);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 44));
    }];
    [logoutBtn setTitleColor:[UIColor colorWithHexString:@"ff556d"] forState:UIControlStateNormal];
    logoutBtn.titleLabel.font=[UIFont systemFontOfSize:14.0];
    logoutBtn.layer.cornerRadius=0;
   
    
    
    
    
}

-(void)someSet:(UIButton *)btn
{
    [btn setTitleColor:[UIColor color3] forState:UIControlStateNormal];
    btn.titleLabel.font=[UIFont systemFontOfSize:14.0];
    btn.layer.cornerRadius=0;
    
    // 1. 得到imageView和titleLabel的宽、高
    CGFloat imageWith = btn.imageView.frame.size.width;
    CGFloat labelWidth = 0.0;
    labelWidth = btn.titleLabel.intrinsicContentSize.width;
    // 2. 声明全局的imageEdgeInsets和labelEdgeInsets
    UIEdgeInsets imageEdgeInsets = UIEdgeInsetsZero;
    UIEdgeInsets labelEdgeInsets = UIEdgeInsetsZero;
    imageEdgeInsets = UIEdgeInsetsMake(0, 15-(SCREEN_WIDTH-labelWidth-imageWith)/2, 0, (SCREEN_WIDTH-labelWidth-imageWith)/2-15);
    labelEdgeInsets = UIEdgeInsetsMake(0, 15-(SCREEN_WIDTH-labelWidth-imageWith)/2+9, 0, (SCREEN_WIDTH-labelWidth-imageWith)/2-15-9);
    // 4. 赋值
    btn.titleEdgeInsets = labelEdgeInsets;
    btn.imageEdgeInsets = imageEdgeInsets;
    
    
    
    UIImageView *tapImg=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"右箭头"]];
    [btn addSubview:tapImg];
    [tapImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-15);
        make.centerY.mas_equalTo(btn);
    }];
    
    
    
//    UILabel *nameLab=[SingLeton myLabel:[UIColor colorWithHexString:@"008bfa"] andTitle:@"填写姓名" andFont:14.0];
//    [btn addSubview:nameLab];
//    [nameLab mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.right.mas_equalTo(tapImg.mas_left).mas_offset(-2);
//        make.centerY.mas_equalTo(btn);
//    }];
    
    
    
    
}
-(void)getInfo
{
        NSString *URLString = @"/axq/admin/get";
    
        [NetworkPath requestWithMethod:0 andURLString:URLString andParameters:nil RequestSuccess:^(NSURLResponse *response, id responseObject) {
            LRLog(@"%@",responseObject);
            NSString *code = responseObject[@"code"];
            if([code isEqualToString:@"10008"]){
                NSDictionary *data=responseObject[@"data"];
                UserModel *model=[UserModel new];
                model.addtime=[NSString stringWithFormat:@"%@",data[@"addtime"]];
                model.updatetime=[NSString stringWithFormat:@"%@",data[@"updatetime"]];
                model.lat=[NSString stringWithFormat:@"%@",data[@"lat"]];
                model.lon=[NSString stringWithFormat:@"%@",data[@"lon"]];
                model.password=[NSString stringWithFormat:@"%@",data[@"password"]];
                model.telephone=[NSString stringWithFormat:@"%@",data[@"telephone"]];
                model.userCode=[NSString stringWithFormat:@"%@",data[@"userCode"]];
                model.userRole=[NSString stringWithFormat:@"%@",data[@"userRole"]];
                model.userTitle=[NSString stringWithFormat:@"%@",data[@"userTitle"]];
                model.address=[NSString stringWithFormat:@"%@",data[@"address"]];
                model.contacts=[NSString stringWithFormat:@"%@",data[@"contacts"]];
                model.headimg=[NSString stringWithFormat:@"%@",data[@"headimg"]];
                model.someId=[NSString stringWithFormat:@"%@",data[@"id"]];
                model.linkman=[NSString stringWithFormat:@"%@",data[@"linkman"]];
                model.loginId=[NSString stringWithFormat:@"%@",data[@"loginId"]];
                model.status=[NSString stringWithFormat:@"%@",data[@"status"]];
                
                uid=model.someId;
                
                
                /** 用户头像更新 **/
                NSURL *babyUrl=[NSURL URLWithString:model.headimg];
                
                UIImage *babyImage=[UIImage imageNamed:@"默认头像"];
                if ([NSString isNULLString:data[@"headimg"]]) {
                    [_photoButton setBackgroundImage:babyImage forState:UIControlStateNormal];
                }else
                {
                    [_photoButton.imageView sd_setImageWithURL:babyUrl placeholderImage:babyImage completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                        [_photoButton setBackgroundImage:_photoButton.imageView.image forState:UIControlStateNormal];
                    }];
                }

                [_nameButton setTitle:model.userTitle forState:UIControlStateNormal];
                _phoneLabel.text=model.loginId;
                
                
                self.tableView.className=@"Type2";
                NSMutableArray *itemArr=[NSMutableArray array];
                
                for (int i=0; i<secArr.count; i++) {
                    cellData *dataObj=[[cellData alloc]init];
                    dataObj.image=secArr[i];
                    dataObj.text=secArr[i];
                    dataObj.detailText=secArr[i];
                    if ([secArr[i] isEqualToString:@"门店"]) {
                        dataObj.text=@"门店名称";
                        dataObj.detailText=model.userTitle;
                    }else if ([secArr[i] isEqualToString:@"民警姓名"])
                    {
                        dataObj.image=@"姓名";
                        dataObj.detailText=model.userTitle;
                    }
                    else if ([secArr[i] isEqualToString:@"物业名称"])
                    {
                        dataObj.image=@"物业";
                        dataObj.detailText=model.userTitle;
                    }
                    else if ([secArr[i] isEqualToString:@"联系人"])
                    {
                        dataObj.detailText=model.contacts;
                    }
                    else if ([secArr[i] isEqualToString:@"联系电话"])
                    {
                        dataObj.detailText=model.telephone;
                    }
                    else if ([secArr[i] isEqualToString:@"地址"])
                    {
                        dataObj.detailText=model.address;
                    }
                    else if ([secArr[i] isEqualToString:@"电动车厂商"])
                    {
                        dataObj.detailText=model.contacts;
                    }
                    
                    
                    [itemArr addObject:dataObj];
                }
                [self.tableView.dataArray addObject:itemArr];
                [self.tableView reloadData];
                
            }
            else
            {
                NSString *str=responseObject[@"msg"];
                
                [self.view makeToast:str];
            }
    
        } RequestFailed:^(NSError *error) {
    
        }];
}


@end
